$.namespace('sys.role');

sys.role = function() {
	var url = '',method = '';
	return {
		init : function() {
			sys.role.initGrid();
			
			$('#isSys').combobox({
				valueField : 'code',
				textField : 'name',
				url : ctxRest + '/sys/basecode/get?classCode=yes_or_no',
				editable : false,
				method : 'get'
			});
			
			$('#permissions').combotree({
				url : ctxRest + '/sys/menu/list',
				idFiled : 'id',
				textFiled : 'name',
				lines : true,
				cascadeCheck: false,
				multiple : true,
				parentField : 'parentId',
				onCheck: function (node, checked) {
					if (checked) {
	                    var parentNode = $("#permissions").combotree('tree').tree('getParent', node.target);
	                    if (parentNode != null) {
	                        $("#permissions").combotree('tree').tree('check', parentNode.target);
	                    }
	                } else {
	                    var childNode = $("#permissions").combotree('tree').tree('getChildren', node.target);
	                    if (childNode.length > 0) {
	                        for (var i = 0; i < childNode.length; i++) {
	                            $("#permissions").combotree('tree').tree('uncheck', childNode[i].target);
	                        }
	                    }
	                }
				},
				onLoadSuccess : function(node, data){
				}
			});
			
		},
		initGrid : function() {
			$('#grid').datagrid({
				//title : '角色列表',
				url : ctxRest + '/sys/role/list',
				fit : true,
				fitColumns : true,
				rownumbers : true,
				nowrap : true,
				striped : true,
				remoteSort : false,
				singleSelect : true,
				border : false,
				idField : 'id',
				columns : [ [ {
					title : '角色名称',
					field : 'name',
					width : 100
				}, {
					title : '英文名称',
					field : 'enname',
					width : 100
				}, {
					title : '角色类型',
					field : 'type',
					width : 80
				}, {
					title : '是否是系统数据',
					field : 'isSys',
					width : 80,
					formatter : function(value, row, index){
						if (value == '1') {
							return '是';
						} else {
							return '否';
						}
					}
				}, {
					title : '备注',
					field : 'remark',
					width : 120
				} ] ],
				toolbar : '#toolbar',
				onDblClickRow : function(index, row) {
					sys.role.showEditForm();
				}
			});
		},
		showForm : function() {
			$('#fmDlg').show().dialog('open');
			$('#fm').form('clear');
			url = ctxRest + '/sys/role/create';
			method = 'POST';
		},
		showEditForm : function() {
			var row = $('#grid').datagrid('getSelected');
			if (!row) {
				$.messager.alert('提示', '请选择要修改的角色.', 'info');
				return;
			}
			url = ctxRest + '/sys/role/create';
			method = 'POST';
			$('#fmDlg').show().dialog('open');
			$('#fm').form('clear');
			$.ajax({
				type : 'Get',
				url : ctxRest + '/sys/role/get?id=' + row.id,
				success : function(result){
					$('#fm').form('load', result);
					if (result.menuList) {
						var menus = [];
						for (var i = 0;i < result.menuList.length;i++) {
							menus.push(result.menuList[i].id);
						}
						$('#permissions').combotree('setValues', menus);
					}
				}
			});
		},
		getFormData : function(){
			var obj;
			var fm = $('#fm');
			if (fm.form('validate')) {
				obj = $.serializeObject(fm);
				if (obj.permissions) {
					obj.menuList = [];
					var menus = obj.permissions.split(',');
					obj.permissions = undefined;
					for (var i = 0;i < menus.length;i++) {
						obj.menuList.push({id : menus[i]});
					}
				}
			}
			return obj;
		},
		saveOrupdate : function() {
			var obj = sys.role.getFormData();
			if (obj) {
				progressLoad();
				$.ajax({
					type : method,
					url : url,
					contentType : 'application/json;charset=UTF-8',
					data : JSON.stringify(obj),
					success : function(result){
						progressClose();
						if (result && result.errcode == 0) {
							$('#fmDlg').dialog('close');
							$('#grid').datagrid('reload');
						}
					}
				})
			}
		},
		remove : function() {
			var row = $('#grid').datagrid('getSelected');
			if (!row) {
				$.messager.alert('提示', '请选择要删除的角色.', 'info');
				return;
			}
			$.messager.confirm('提示', '您确定要删除?', function(flag){
				if (flag) {
					progressLoad();
					$.ajax({
						type : 'GET',
						url : ctxRest + '/sys/role/delete?id=' + row.id,
						success : function(result){
							progressClose();
							if (result && result.errcode == 0) {
								$('#grid').datagrid('reload');
							} else if (result.errcode == 1){
								$.messager.alert('提示', result.msg, 'info');
							}
						}
					});
				}
			});
		}
	};
}();

$(function() {
	sys.role.init();
})